<% layout('/layouts/default.html', {title: '药材赋码信息管理', libs: ['validate']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(ownMedicineAssignment.isNewRecord ? '新增药材赋码信息' : '编辑药材赋码信息')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${ownMedicineAssignment}" action="${ctx}/own/ownMedicineAssignment/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="id"/>
				<#form:hidden id="ownMedicineReceiptInformationId" path="ownMedicineReceiptInformationId"/>
				<#form:hidden id="ownMedicineSubcontractingId" path="ownMedicineSubcontractingId"/>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('药材批次号')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:listselect id="medicineBatchId" title="药材批次号" labelPath="medicineBatchId"
								url="${ctx}/own/ownMedicineInspectionInformation/findByKey" allowClear="false" checkbox="false"
								itemCode="medicineBatchId" itemName="medicineBatchId" class="form-control required"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('区域编码')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input id="areaCode" path="areaCode" maxlength="6" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('品名')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input id="varietyName" path="varietyName" maxlength="50" class="form-control"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('商品编码')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="sourceCode" maxlength="50" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
				<% if (hasPermi('own:ownMedicineAssignment:edit') && ownMedicineAssignment.isNewRecord){ %>
					<div class="row">
						<div class="col-xs-6">
							<div class="form-group">
								<label class="control-label col-sm-4" title="">
									<span class="required hide">*</span> ${text('药材剩余重量(g)')}：<i class="fa icon-question hide"></i></label>
								<div class="col-sm-8">
									<#form:input id="residualWeight" class="form-control number"/>
								</div>
							</div>
						</div>
						<div class="col-xs-6">
							<div class="form-group">
								<label class="control-label col-sm-4" title="">
									<span class="required hide">*</span> ${text('此次分包重量(g)')}：<i class="fa icon-question hide"></i></label>
								<div class="col-sm-8">
									<#form:input id="thisWeight" type="number" class="form-control required"/>
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-xs-6">
							<div class="form-group">
								<label class="control-label col-sm-4" title="">
									<span class="required hide">*</span> ${text('包数')}：<i class="fa icon-question hide"></i></label>
								<div class="col-sm-8">
									<#form:input id="numPackage" class="form-control number"/>
								</div>
							</div>
						</div>
					</div>&nbsp;
				<% } %>
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('规格')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="spec" maxlength="10" class="form-control"/>
								<!--<#form:radio path="spec" dictType="own_out_spec" class="form-control" />-->
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('重量(g)')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="weight" class="form-control number"/>
							</div>
						</div>
					</div>
				</div>
				<!--<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('赋码时间')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="time" readonly="true" maxlength="20" class="form-control Wdate"
									dataFormat="datetime" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:true});"/>
							</div>
						</div>
					</div>
				</div>-->
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('备注')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="remarks" rows="4" maxlength="255" class="form-control"/>
							</div>
						</div>
					</div>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('own:ownMedicineAssignment:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
	let residualWeight  = 0;
	$("#inputForm").validate({
		submitHandler: function(form){
			js.ajaxSubmitForm($(form), function(data){
				js.showMessage(data.message);
				if(data.result == Global.TRUE){
					js.closeCurrentTabPage(function(contentWindow){
						contentWindow.page();
					});
				}
			}, "json");
		}
	});
	//选择药材批次号回调方法
	function  listselectCallback(id, act, index, layero, selectData) {
		if (id == 'medicineBatchId' && (act == 'ok' || act == 'clear')){
			for (let key in selectData){
				$('#ownMedicineReceiptInformationId').val(selectData[key]['ownMedicineReceiptInformationId']);
				residualWeight = selectData[key]['residualWeight'];
				$('#residualWeight').val(selectData[key]['residualWeight']);
				$("#thisWeight").removeAttr("readonly");
				$("#numPackage").removeAttr("readonly");
				$.ajax({
					url: "${ctx}/own/ownMedicineReceiptInformation/findById",
					type: "get",
					data: {"id": selectData[key]['ownMedicineReceiptInformationId']},
					dataType: "json",
					success: function(data){
						if (data.success){
							$("#varietyName").val(data.data.varietyName);
							$("#areaCode").val(data.data.areaCode);
						}else {
							js.showErrorMessage("获取失败，错误信息"+data.msg+"!");
							console.log();
						}
					},
					error: function(e){
						js.showErrorMessage("错误: "+e);
					}

				})
				break;
			}
		}
	}
	$(function(){
		$("#residualWeight").attr("readonly","readonly");
		$("#thisWeight").attr("readonly","readonly");
		$("#numPackage").attr("readonly","readonly");
		$("#weight").attr("readonly","readonly");
		var jsbeet = "${ownMedicineAssignment.isNewRecord}"; //获取beetl的值
		if (jsbeet == 'false')
			$("#medicineBatchIdName").attr("disabled","disabled");

		//输入框的值改变时触发
		$("#thisWeight").on("input",function(e){
			$("#residualWeight").val(residualWeight);
			//获取input输入的值
			if (e.delegateTarget.value < 0){
				e.delegateTarget.value = 0;
			}
			if (e.delegateTarget.value > residualWeight){
				e.delegateTarget.value = residualWeight;
			}
			if (e.delegateTarget.value >= 0 && $("#residualWeight").val() != '' && $("#residualWeight").val() > 0){
				var diff = $("#residualWeight").val() - e.delegateTarget.value;
				if (diff < 0){
					js.showErrorMessage("剩余重量不够！");
				}else {
					$("#residualWeight").val(diff);
					if ($("#numPackage").val() != '' && $("#numPackage").val() > 0){
						$("#weight").val(e.delegateTarget.value / $("#numPackage").val());
					}
				}
			}
		});
		$("#numPackage").on("input",function(e){
			//获取input输入的值
			if (e.delegateTarget.value < 1){
				e.delegateTarget.value = 1;
			}
			if (e.delegateTarget.value >= 1 && $("#thisWeight").val() != '' && $("#thisWeight").val() > 0){
				$("#weight").val($("#thisWeight").val() / e.delegateTarget.value);
			}
		});
	});
</script>